package com.imut.oa.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imut.oa.common.PageResult;
import com.imut.oa.mapper.AttendanceMapper;
import com.imut.oa.pojo.dto.AttendancePageQueryDTO;
import com.imut.oa.pojo.dto.AttendanceRemarkDTO;
import com.imut.oa.pojo.dto.EmployeeAttendanceDTO;
import com.imut.oa.pojo.dto.WorkTimeConfigDTO;
import com.imut.oa.pojo.entity.Attendance;
import com.imut.oa.pojo.vo.AttendanceRecordVO;
import com.imut.oa.pojo.vo.AttendanceVO;
import com.imut.oa.pojo.vo.LeaveVO;
import com.imut.oa.service.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
        implements AttendanceService {
    @Autowired
    private  AttendanceMapper attendanceMapper;
    @Override
    public PageResult page(AttendancePageQueryDTO attendancePageQueryDTO) {
        // 创建条件对象，不设置任何条件，相当于查询所有
        Page<AttendanceVO> page = new Page<>(attendancePageQueryDTO.getPage(), attendancePageQueryDTO.getPageSize());
        // 调用自定义的联表分页查询
        Page<AttendanceVO> voPage = attendanceMapper.selectAttendancePage(page, attendancePageQueryDTO);
        System.out.println(voPage);
        return new PageResult(voPage.getTotal(), voPage.getRecords());
    }

    @Override
    public void updateRemark(AttendanceRemarkDTO attendanceRemarkDTO) {
        if (attendanceRemarkDTO.getId() == null) {
            throw new RuntimeException("考勤id不能为空");
        }
        if (attendanceRemarkDTO.getRemark() == null) {
            throw new RuntimeException("备注不能为空");
        }
        attendanceMapper.updateRemark(attendanceRemarkDTO);
    }

    @Override
    public void attendance(EmployeeAttendanceDTO employeeAttendanceDTO) {
        if (employeeAttendanceDTO.getEmployeeId() == null) {
            throw new RuntimeException("员工id不能为空");
        }
        if (employeeAttendanceDTO.getAttendanceType() == null) {
            throw new RuntimeException("考勤类型不能为空");
        }
        if (employeeAttendanceDTO.getClockTime() == null) {
            throw new RuntimeException("打卡时间不能为空");
        }
        if (employeeAttendanceDTO.getLatitude() == null) {
            throw new RuntimeException("纬度不能为空");
        }
        if (employeeAttendanceDTO.getLongitude() == null) {
            throw new RuntimeException("经度不能为空");
        }
        if (employeeAttendanceDTO.getStatus() == null) {
            throw new RuntimeException("考勤状态不能为空");
        }
        attendanceMapper.attendance(employeeAttendanceDTO);
    }

    @Override
    public List<AttendanceRecordVO> getTodayRecords(Long currentId) {
        if (currentId == null) {
            throw new RuntimeException("员工id不能为空");
        }
        // 生成当天时间范围
        LocalDateTime todayStart = LocalDate.now().atStartOfDay();
        LocalDateTime todayEnd = LocalDate.now().atTime(23, 59, 59);

        return attendanceMapper.findByEmployeeAndDateRange(
                currentId,
                todayStart,
                todayEnd
        );
    }
    @Override
    public WorkTimeConfigDTO getWorkTimeConfig() {
        return attendanceMapper.getWorkTimeConfig();
    }
    @Override
    public int updateWorkTimeConfig(WorkTimeConfigDTO workTimeConfigDTO) {
        return attendanceMapper.updateWorkTimeConfig(workTimeConfigDTO);
    }
    @Override
    public List<AttendanceRecordVO> getAttendanceRecords(Long employeeId, int page, int pageSize) {
        if (employeeId == null) {
            throw new RuntimeException("员工id不能为空");
        }
        int offset = (page - 1) * pageSize;
        return attendanceMapper.getAttendanceRecords(employeeId, offset, pageSize);
    }
}
